<!--
 * @Author: zl
 * @Date: 2021-02-09 15:24:23
 * @LastEditTime: 2021-11-30 18:35:12
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
-->
<template>
    <el-dialog
        :title="title"
        append-to-body
        destroy-on-close
        :model-value="showDialog"
        @close="closeDialog()"
    >
        <!-- <span>{{ rowData }}</span> -->
        <!-- 表单 -->
        <el-form
            :model="form"
            ref="formRef"
            :rules="rules"
            label-width="80px"
            :inline="false"
        >
            <el-form-item label="村庄名称" prop="villageCode">
                <el-select
                    style="width: 100%"
                    v-model="villageCode"
                    placeholder="村庄名称"
                    clearable
                >
                    <el-option
                        v-for="item in villageType"
                        :key="item.code"
                        :label="item.name"
                        :value="item.code"
                        :disabled="item.villageCode == 0"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="姓名" prop="name">
                <el-input
                    v-model="name"
                    placeholder="姓名"
                    clearable
                ></el-input>
            </el-form-item>
            <el-form-item label="手机号" prop="phone">
                <el-input
                    v-model="phone"
                    placeholder="手机号"
                    clearable
                ></el-input>
            </el-form-item>
            <el-form-item label="居民类型" prop="type">
                <el-select
                    style="width: 100%"
                    v-model="type"
                    placeholder="居民类型"
                    clearable
                >
                    <el-option
                        v-for="item in jmType"
                        :key="item.code"
                        :label="item.name"
                        :value="item.code"
                        :disabled="item.type == 0"
                    ></el-option>
                </el-select>
            </el-form-item>
        </el-form>

        <template v-slot:footer>
            <span>
                <el-button @click="closeDialog()">取消</el-button>
                <el-button type="primary" @click="onSubmit()">确定</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script setup>
import {
    reactive,
    toRefs,
    ref,
    onMounted,
    defineProps,
    defineEmits,
} from "vue";
const rules = {
    villageCode: [
        {
            required: true,
            message: "请选择村庄",
            trigger: "blur",
        },
    ],
    name: [
        {
            required: true,
            message: "请输入姓名",
            trigger: "blur",
        },
    ],
    phone: [
        {
            required: true,
            message: "请输入手机号",
            trigger: "blur",
        },
    ],
    type: [
        {
            required: true,
            message: "请选择居民类型",
            trigger: "blur",
        },
    ],
};
const props = defineProps({
    showDialog: {
        type: Boolean,
        default: true,
    },
    title: {
        type: String,
        default: "添加",
    },
    rowData: {
        type: Object,
        default: null,
    },
});
const emit = defineEmits(["closeDialog"]);
const { title, rowData } = toRefs(props);
const closeDialog = () => {
    emit("closeDialog", false);
};
const formRef = ref(null);
const form = reactive({
    name: "",
    userId: sessionStorage.getItem("unameid"),
    phone: "",
    villageCode: "",
    type: "",
});
const { name, phone, villageCode, type } = toRefs(form);

const villageType = ref([]);
const jmType = ref([]);

/**
 * @description: 初始化
 * @param {*}
 * @return {*}
 */
rowData.value &&
    ((name.value = rowData.value.name),
    (phone.value = rowData.value.phone),
    (type.value = rowData.value.type),
    (villageCode.value = rowData.value.villageCode));
/**
 * @description: 获取角色列表
 * @param {*}
 * @return {*}
 */
const getVillageTypeList = async () => {
    const { code, data } = await VE_API.system.dictionaryEntityByCode({
        dicCode: "czxx",
    });
    if (code == "00") {
        villageType.value = data;
    }
};
/**
 * @description: 获取角色列表
 * @param {*}
 * @return {*}
 */
const getJmTypeList = async () => {
    const { code, data } = await VE_API.system.dictionaryEntityByCode({
        dicCode: "jmlx",
    });
    if (code == "00") {
        jmType.value = data;
    }
};

/**
 * @description:提交
 * @param {*}
 * @return {*}
 */
const onSubmit = () => {
    formRef.value.validate(async (valid) => {
        if (valid) {
            let res;
            if (title.value == "添加") {
                res = await VE_API.system.villagePhoneAdd(form);
            } else {
                res = await VE_API.system.villagePhoneEdit({
                    id: rowData.value.id,
                    ...form,
                });
            }
            const { code } = res;
            if (code == "00") {
                closeDialog();
            }
        } else {
            console.log("error submit!!");
            return false;
        }
    });
};
onMounted(async () => {
    await getVillageTypeList();
    await getJmTypeList();
    // maxHeight(pagination, queryForm, toolBar, ve_max_height);
});
</script>

<style lang="scss" scoped></style>
